﻿using GetData.webProductClass;
using jutehui.GetData;
using jutehui.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace GetData
{
    public class liuzhuni
    {
        /// <summary>
        /// 通过网页html获取商品信息并保存到数据库中
        /// </summary>
        /// <param name="url"></param>
        /// <param name="shoppingType">海淘2 精选1</param>
        /// <returns></returns>
        public string GetProductsToDataBase(string url)
        {
            //  string url = "http://www.shihuo.cn/";
            var htmlText = CustomMethod.GetHtmlByHttpHelper(url);

            var pList = GetProductByRegex(htmlText);
            if (pList == null)
            {
                return "";
            }
            string msg = "";
            DataTable dt = CustomMethod.ListToDataTable<Product>(pList, "Product", ref msg);
            if (dt == null || dt.Rows.Count < 1)
            {
                return "";
            }
            jutehui.BLL.Product bll = new jutehui.BLL.Product();

            if (bll.SaveTableData("Product", dt, out msg))
            {
                return dt.Rows.Count.ToString(); ;
            }
            else
            {
                msg = "留住你：GetProductsToDataBase" + msg;
                CustomMethod.WriteLog(msg);
                return "";
            }
        }

        /// <summary>
        /// 根据html内容解析出商品
        /// </summary>
        /// <param name="htmlText"></param>
        /// <returns></returns>
        public List<Product> GetProductByRegex(string htmlText)
        {
            List<Product> plist = new List<Product>();
            ProductTypeEnum pEnum = new ProductTypeEnum();

            //正则获取每一个商品的html描述
            string pRegex = @"<div class=""list_pic fl"">[\s\S]*?<div class=""zhi_list cf"">";
            var list = CustomMethod.GetInfoByRegex(pRegex, htmlText, 0);
            if (list == null || list.Count < 1)
            {
                string msg = "留住你：GetProductByRegex ，没有抓到匹配的商品，检查匹配规则。";
                CustomMethod.WriteLog(msg);
                return null;
            }
            for (int index = 0; index < list.Count; index++)
            {
                Product product = new Product();
                try
                {
                    //-------------------初始值------------------------------------------
                    product.FromWeb = "留住你";
                    product.TypeId = (int)MallsEnumClass.NavigationType.精选;
                    product.UpdateTime = DateTime.Now;
                    product.UpdateTimeStr = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    //--------------------根据网页解析商品相关信息---------------------------------  
                    //获取所有herf连接及内容
                    var herfList = CustomMethod.GetInfoFroma(list[index]);
                    if (herfList == null)//|| herfList.Count > 30  || herfList.Count < 13
                    {
                        continue;
                    }
                    //---------------------------解析商品标签-----------
                    string labelUrl = "http://www.liuzhuni.com" + herfList[0];
                    string labelText = CustomMethod.GetHtmlByHttpHelper(labelUrl);
                    string labelRegex = @"<div class=""bread_crumbs"">[\s\S]*?</div>";

                    product.ProductTypeId = pEnum.GetProductTypeId(labelText, labelRegex, typeof(ProductTypeEnum.liuzhuniType),0);

                    //获取图片链接 data-original 
                    string srcRegex = "";
                    if (herfList[1].Contains("data-original"))
                    {
                        srcRegex = @"data-original=[""|']([^""|']*)[""|']";
                    }
                    else
                    {
                        srcRegex = @"src=[""|']([^""|']*)[""|']";
                    }
                    var srcImg = CustomMethod.GetInfoByRegex(srcRegex, herfList[1], 1);
                    if (srcImg == null || srcImg.Count < 1)
                    {
                        CustomMethod.WriteLog("留住你：没有获取图片链接");
                        continue;
                    }
                    product.ImgUrl = srcImg[0];
                    product.Pname = Regex.Replace(herfList[3], @"<(.[^>]*)>", "");
                    product.Price = herfList[5];
                    //  product.Source = herfList[herfList.Count - 5];
                    string souRegex = @"<p class=""list_label"">[\s\S]*?</p>";
                    var souConten = CustomMethod.GetInfoByRegex(souRegex, list[index], 0);
                    if (souConten != null && souConten.Count > 0)
                    {
                        var source = CustomMethod.GetInfoFroma(souConten[0]);
                        if (source.Count > 0)
                        {
                            product.Source = source[1];
                        }
                    }

                    //获取商品源url  跳转url
                    string detailUrl = "http://www.huim.com/" + herfList[herfList.Count - 2];
                    string sourceUrl = "";
                    string ldUrl = "";
                    if (herfList[herfList.Count - 2].Contains("http"))
                    {
                        detailUrl = herfList[herfList.Count - 2];
                        ldUrl = MallsEnumClass.GetSourceUrl(detailUrl, ref sourceUrl);
                    }
                    else
                    {
                        ldUrl = CustomMethod.GetTrueUrl(detailUrl, ref sourceUrl);

                    }
                    if (ldUrl == "")
                    {
                        if (sourceUrl == "")
                        {
                            CustomMethod.WriteLog("今日聚超值：GetProductByRegex 网站没有解析出商品链接，请检查规则。");
                        }
                        continue;
                    }
                    product.Plink = ldUrl;
                    product.Memo = sourceUrl;

                    ////获取描述信息
                    //string desRegex = @"<div class=""detail_dec"">[\s\S]*?</div>";
                    //var divContent = CustomMethod.GetInfoByRegex(desRegex, list[index], 0);
                    //if (divContent == null || divContent.Count < 1 || divContent[0].Equals(""))
                    //{
                    //    product.Description = product.Pname;
                    //}
                    //else
                    //{
                    //    product.Description = divContent[0];
                    //    //string desContentRegex = @"<p>[\s\S]*?</p>";
                    //    //string desCont = "";
                    //    //var desContentList = CustomMethod.GetInfoByRegex(desContentRegex, divContent[0], 0);
                    //    //for (int count = 0; count < desContentList.Count; count++)
                    //    //{
                    //    //    desCont += Regex.Replace(desContentList[count], @"<(.[^>]*)>", "") + "|||";
                    //    //}
                    //    //product.Description = desCont == "" ? product.Pname : desCont;
                    //}


                    product.Pname = product.Pname = System.Web.HttpUtility.HtmlDecode(product.Pname);// product.Pname.Replace("&#x2F;", "/").Replace("#039;", "'").Replace("&amp;", "&").Replace("&#x27;", "'").Replace("&quot;", "\""); 
                  //  product.Description.Replace("&#x2F;", "/").Replace("#039;", "'");
                }
                catch (Exception ex)
                {
                    string msg = "留住你：GetProductByRegex " + ex.Message.ToString() + ":" + index;
                    CustomMethod.WriteLog(msg);
                    continue;
                }
                plist.Add(product);

            }
            var newList = CustomMethod.GetDistinctProdcuts(plist);
            newList.Reverse();
            return newList;
        }
    }
}
